#include "qremovepagedialog.h"
#include "ui_qremovepagedialog.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QItemDelegate>
#include <QStringListModel>
#include <QPainter>

struct tagPageInfo
{
    QString m_name;
    bool    m_checked;
};

class QPageModel: public QStringListModel
{
public:
    Qt::ItemFlags flags ( const QModelIndex & index ) const ;

    bool        setData(const QModelIndex &index, const QVariant &value, int role);
    QVariant    data(const QModelIndex &index, int role) const;

    int         rowCount(const QModelIndex &parent) const;

    void        addItems(QStringList names);

    QStringList getSelectNames();

protected:
    QList<tagPageInfo>  m_items;
};

Qt::ItemFlags QPageModel::flags(const QModelIndex &index) const
{
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

int QPageModel::rowCount(const QModelIndex &parent) const
{
    return m_items.size();
}

void QPageModel::addItems(QStringList names)
{
    beginInsertRows(QModelIndex(),m_items.size(),names.size()+m_items.size()-1);
    foreach(QString name,names)
    {
        tagPageInfo info;
        info.m_name=name;
        info.m_checked=false;
        m_items.append(info);
    }
    endInsertRows();
}

QStringList QPageModel::getSelectNames()
{
    QStringList list;
    foreach(tagPageInfo info,m_items)
    {
        if(info.m_checked)
        {
            list.append(info.m_name);
        }
    }
    return list;
}

bool QPageModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(index.row()<0 || index.row()>=m_items.size())
    {
        return false;
    }
    tagPageInfo info=m_items.at(index.row());

    switch(role)
    {
    case Qt::DisplayRole:
        info.m_name=value.toString();
        break;
    case Qt::UserRole:
        info.m_checked=value.toBool();
        break;
    }
    m_items.replace(index.row(),info);
    return true;
}

QVariant QPageModel::data(const QModelIndex &index, int role) const
{
    if(index.row()<0 || index.row()>=m_items.size())
    {
        return QVariant();
    }
    tagPageInfo info=m_items.at(index.row());

    switch(role)
    {
    case Qt::DisplayRole:
        return info.m_name;
        break;
    case Qt::UserRole:
        return info.m_checked;
        break;
    }
    return QVariant();
}

class QPageDelegate: public QItemDelegate
{
public:
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

QSize QPageDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize sz=QItemDelegate::sizeHint(option,index);
    sz.setHeight(25);
    return sz;
}

void QPageDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QString text=index.data(Qt::DisplayRole).toString();
    bool    checked=index.data(Qt::UserRole).toBool();


    QStyleOptionButton opt;
    opt.state |=checked?QStyle::State_On:QStyle::State_Off;

    opt.state |=QStyle::State_Enabled;

    drawCheck(painter,option,QRect(option.rect.x()+4,option.rect.y()+4,16,16),checked?Qt::Checked:Qt::Unchecked);

    QStyleOptionViewItem itemOpt;
    itemOpt=option;
    QFont f=itemOpt.font;
    f.setPointSize(13);
    itemOpt.font=f;
    itemOpt.fontMetrics=QFontMetrics(f);
    drawDisplay(painter,option,itemOpt.rect.adjusted(20,0,4,0),text);

    painter->setPen(QColor(80,80,80));
    painter->drawLine(0,option.rect.bottom(),option.rect.right(),option.rect.bottom());
}


QRemovePageDialog::QRemovePageDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QRemovePageDialog),
    m_list(new QListView),
    m_button(new QPushButton(this)),
    m_model(new QPageModel)
{
    ui->setupUi(this);

    m_list->setModel(m_model);
    m_list->setItemDelegate(new QPageDelegate);

    connect(m_list,SIGNAL(pressed(QModelIndex)),this,SLOT(clickItem(QModelIndex)));

    QVBoxLayout *l=new QVBoxLayout;

    l->setMargin(0);
    l->setSpacing(0);
    l->addWidget(m_list);

    QWidget *wid=new QWidget;

    wid->setFixedHeight(50);

    l->addWidget(wid);

    QHBoxLayout *h=new QHBoxLayout;
    wid->setLayout(h);
    m_button->setFixedSize(75,30);
    m_button->setText("Remove");

    h->addWidget(m_button);

    this->setLayout(l);


    connect(m_button,SIGNAL(clicked()),this,SLOT(removeBtn()));
}

QRemovePageDialog::~QRemovePageDialog()
{
    delete ui;
}

void QRemovePageDialog::setPageNames(QStringList names)
{
    m_model->addItems(names);
}

void QRemovePageDialog::clickItem(const QModelIndex &index)
{
    m_model->setData(index,!index.data(Qt::UserRole).toBool(),Qt::UserRole);
    m_list->update(index);
}

void QRemovePageDialog::removeBtn()
{
    m_removeNames=m_model->getSelectNames();
    close();
}

QStringList QRemovePageDialog::getRemoveNames()
{
    return m_removeNames;
}
